Attribute VB_Name = "modFriendSystem"

Public Sub AddFriend(ByVal PlayerIndex As Long, ByVal FriendIndex As Long)
    
    With Account(PlayerIndex)
        .NumOfFriends = .NumOfFriends + 1
        .Friends(.NumOfFriends) = GetPlayerLogin(FriendIndex)
    End With
    With Account(FriendIndex)
        .NumOfFriends = .NumOfFriends + 1
        .Friends(.NumOfFriends) = GetPlayerLogin(PlayerIndex)
    End With
    
    UpdateFriendList PlayerIndex
    UpdateFriendList FriendIndex
End Sub

Public Sub RemoveFriend(ByVal PlayerIndex As Long, ByVal FriendIndex As Long)
    Dim i As Byte
    
    If FriendIndex <= 0 Or FriendIndex > Account(PlayerIndex).NumOfFriends Then Exit Sub
    
    With Account(PlayerIndex)
        .Friends(FriendIndex) = vbNullString
        If FriendIndex < .NumOfFriends Then
            For i = FriendIndex To .NumOfFriends - 1
                .Friends(i) = .Friends(i + 1)
            Next
        End If
        .NumOfFriends = .NumOfFriends - 1
    End With
    
End Sub

Public Sub UpdateFriendList(ByVal index As Long)
    Dim buffer As clsBuffer
    Dim i As Byte
    Dim FriendIndex As Byte
    
    Set buffer = New clsBuffer
    
    buffer.WriteLong SUpdateFriendList
    buffer.WriteByte Account(index).NumOfFriends
    If Account(index).NumOfFriends > 0 Then
        For i = 1 To Account(index).NumOfFriends
            FriendIndex = FindAccount(Account(index).Friends(i))
            buffer.WriteString Account(index).Friends(i)
            If FriendIndex > 0 Then
                buffer.WriteByte 1
            Else
                buffer.WriteByte 0
            End If
        Next
    End If
    
    SendDataTo index, buffer.ToArray()
    
    Set buffer = Nothing
End Sub

Public Function FindFriend(ByVal index As Long, ByVal FriendName As String) As Byte
    Dim i As Byte
    
    If index <= 0 Or index > Player_HighIndex Then
        FindFriend = 0
        Exit Function
    End If
    
    If Account(index).NumOfFriends > 0 Then
        For i = 1 To Account(index).NumOfFriends
            If Trim$(FriendName) = Trim$(Account(index).Friends(i)) Then
                FindFriend = i
                Exit Function
            End If
        Next
    End If
    
    FindFriend = 0

End Function

Public Sub DeleteFriend(ByVal PlayerIndex As Long, ByVal FriendIndex As Byte)
    Dim index As Long
    Dim RemFriend As Byte
    
    RemoveFriend PlayerIndex, FriendIndex
    index = FindPlayer(Account(PlayerIndex).Friends(FriendIndex))
    If index > 0 Then
        RemoveFriend index, FindFriend(index, Account(PlayerIndex).Login)
    Else
        index = Player_HighIndex + 1
        LoadPlayer index, Account(PlayerIndex).Friends(FriendIndex)
        RemFriend = FindFriend(index, Account(PlayerIndex).Login)
        If RemFriend > o Then
            RemoveFriend index, RemFriend
        End If
        ClearPlayer index
    End If
    
    UpdateFriendList PlayerIndex
    UpdateFriendList index
End Sub

Public Sub SendFriendInv(ByVal RecIndex As Long, ByVal SenIndex As Long)
    Dim buffer As clsBuffer
    
    If Account(SenIndex).NumOfFriends >= MAX_PLAYER_FRIENDS Then Exit Sub
    If Account(RecIndex).NumOfFriends >= MAX_PLAYER_FRIENDS Then Exit Sub
    
    Set buffer = New clsBuffer
    
    TempPlayer(RecIndex).FriendInv = SenIndex
    buffer.WriteLong SSendFriendInv
    buffer.WriteLong SenIndex
    SendDataTo RecIndex, buffer.ToArray
    Set buffer = Nothing
End Sub

Public Sub SendFriendOnline(ByVal index As Long)
    Dim buffer As clsBuffer
    Dim i As Byte
    Dim FriendIndex As Long
    Dim FriendIndex2 As Byte
    
    If Account(index).NumOfFriends <= 0 Then Exit Sub

    For i = 1 To Account(index).NumOfFriends
        FriendIndex = FindAccount(Account(index).Friends(i))
        If FriendIndex > 0 Then
            FriendIndex2 = FindFriend(FriendIndex, Account(index).Login)
            If FriendIndex2 > 0 Then
                Set buffer = New clsBuffer
                buffer.WriteLong SFriendOnline
                buffer.WriteByte FriendIndex2
                SendDataTo FriendIndex, buffer.ToArray
                Set buffer = Nothing
            End If
        End If
    Next
End Sub

Public Sub SendFriendOffline(ByVal index As Long)
    Dim buffer As clsBuffer
    Dim i As Byte
    Dim FriendIndex As Long
    Dim FriendIndex2 As Byte
    
    If Account(index).NumOfFriends <= 0 Then Exit Sub
    
    For i = 1 To Account(index).NumOfFriends
        FriendIndex = FindAccount(Account(index).Friends(i))
        If FriendIndex > 0 Then
            FriendIndex2 = FindFriend(FriendIndex, Account(index).Login)
            Set buffer = New clsBuffer
            buffer.WriteLong SFriendOffline
            buffer.WriteByte FriendIndex2
            SendDataTo FriendIndex, buffer.ToArray()
            Set buffer = Nothing
        End If
    Next
End Sub

Public Function IsAlreadyFriend(ByVal index As Long, ByVal FriendName As String) As Boolean
    Dim i As Byte
    
    If Account(index).NumOfFriends > 0 Then
        For i = 1 To Account(index).NumOfFriends
            If Trim$(FriendName) = Trim$(Account(index).Friends(i)) Then
                IsAlreadyFriend = True
                Exit Function
            End If
        Next
    End If
    IsAlreadyFriend = False
End Function
